<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台管理系统登录</title>
    <link rel="stylesheet" href="./style/bootstrap.css">
    <style>
        body {
            background-color: #f0f0f0;
        }

        .login-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin-top: 100px;
        }

        .tips {
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-4 login-container">
                    <h2 class="text-center">后台管理系统登录</h2>
                    <div class="form-group">
                        <label for="username">账号</label>
                        <input v-model="username" type="text" class="form-control" id="username"
                            placeholder="请输入用户名或者手机号">
                        <div class=" tips mt-2" v-if="usernameTips">{{ usernameTips }}</div>
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input v-model="password" type="password" class="form-control" id="password"
                            placeholder="请输入密码">
                        <div class=" tips mt-2" v-if="passwordTips">{{ passwordTips }}</div>
                    </div>
                    <button @click="login" class="btn btn-primary btn-block">登录</button>
                    <div class="text-center tips mt-2" v-if="authorityTips">{{ authorityTips }}</div>
                </div>
            </div>
        </div>
    </div>


    <script src="./js/axios.js"></script>
    <script src="./js/vue2.js"></script>
    <script>
        const login = new Vue({
            el: '#app',
            data: {
                username: '',
                password: '',
                authorityTips: '',
                usernameTips: '',
                passwordTips: '',
                user: [],
                userId: null
            },
            async created() {
                let obj = await axios.get('http://localhost:3888/user');
                this.user = obj.data;
            },
            methods: {
                login() {
                    // 使用 find 方法查找用户，既可以通过用户名或者手机号查找
                    let foundUser = this.user.find(user => user.username === this.username || user.phone === this.username);
                    if (foundUser) {
                        // 找到用户后，检查密码是否正确
                        if (foundUser.password === this.password) {
                            // 密码正确，再检查权限
                            if (foundUser.authority < 3) {
                                alert('登录成功');
                                this.userId = foundUser.id;
                                this.goToPage();
                            } else {
                                this.usernameTips = '';
                                this.passwordTips = '';
                                this.authorityTips = '权限不足';
                            }
                        } else {
                            this.usernameTips = '';
                            this.authorityTips = '';
                            this.passwordTips = '密码错误';
                        }
                    } else {
                        this.passwordTips = '';
                        this.authorityTips = '';
                        this.usernameTips = '用户名或手机号错误';
                    }
                },
                goToPage() {
                    // 这里假设使用 window.location.href 进行页面跳转，将 userId 作为参数传递
                    window.location.href = `./home/home.html?id=${this.userId}`;
                }

            }
        });
    </script>
</body>

</html>